import ky from 'ky'

const api = ky.extend({
  prefixUrl: import.meta.env.VITE_API_BASE as string,
  hooks: {
    /*
    beforeRequest: [
      async (request, options) => {
        const auth = await getAuth()
        if (auth == null) return
        request.headers.set('Authorization', auth)
      }
    ],
    afterResponse: [
      async (request, options, response) => {
        if (response.status !== 401) return
        // refresh token if 401
        const auth = await refreshAuth()
        if (auth == null) return
        request.headers.set('Authorization', auth)
        return await ky(request, options)
      }
    ]
    */
  }
})

const apiGet = async (xpath: string): Promise<any> => {
  const result: any = await api.get(xpath).json()
  if (result.success)
    return result.value
  else {
    console.log('API error', {xpath, result})
    return null
  }
}

export { api, apiGet }
